"use strict";

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.mask = void 0;
exports.maskBankCard = maskBankCard;
exports.maskCellphone = maskCellphone;
exports.maskEmail = maskEmail;
exports.maskFixedPhone = maskFixedPhone;
exports.maskIdCardForce = maskIdCardForce;
exports.maskIdCardNormal = maskIdCardNormal;
exports.maskName = maskName;
/**
 * 基础脱敏函数
 * @param str
 * @param param1
 * @returns
 */
const mask = (str, opts) => {
  const before = opts?.before && opts?.before < 1 && opts?.before > 0 ? Math.floor(str.length * opts?.before) : opts?.before;
  const after = opts?.after && opts?.after < 1 && opts?.after > 0 ? Math.floor(str.length * opts?.after) : opts?.after;
  const reverse = opts?.reverse;
  let result;
  if (before && after) {
    const reg = new RegExp(`([A-Za-z0-9\u4e00-\u9fa5]{${before}})([A-Za-z0-9\u4e00-\u9fa5]*)([A-Za-z0-9\u4e00-\u9fa5]{${after}})`);
    result = str.replace(reg, ($0, $1, $2, $3) => {
      return !reverse ? $1 + $2.replace(/./g, '*') + $3 : $1.replace(/./g, '*') + $2 + $3.replace(/./g, '*');
    });
  } else if (before) {
    const reg = new RegExp(`([A-Za-z0-9\u4e00-\u9fa5]{${before}})([A-Za-z0-9\u4e00-\u9fa5]*)`);
    result = str.replace(reg, ($0, $1, $2) => {
      return !reverse ? $1 + $2.replace(/./g, '*') : $1.replace(/./g, '*') + $2;
    });
  } else if (after) {
    const reg = new RegExp(`([A-Za-z0-9\u4e00-\u9fa5]*)([A-Za-z0-9\u4e00-\u9fa5]{${after}})`);
    result = str.replace(reg, ($0, $1, $2) => {
      return !reverse ? $1.replace(/./g, '*') + $2 : $1 + $2.replace(/./g, '*');
    });
  } else {
    result = str;
  }
  return result;
};

/**
 * 【浙里办脱敏规范】姓名脱敏：隐藏第一位
 * @param name
 */
exports.mask = mask;
function maskName(name) {
  return mask(name, {
    before: 1,
    reverse: true
  });
}

/**
 * 【浙里办脱敏规范】身份证强隐藏，只显示前后一位
 * @param idCard
 */
function maskIdCardForce(idCard) {
  return mask(idCard, {
    before: 1,
    after: 1
  });
}

/**
 * 【浙里办脱敏规范】身份证普通隐藏：显示前 1、5-12、18 位，其余用 * 代替
 * 六位数字地址码+八位数字出生日期码+三位数字顺序码+一位数字校验码
 * 六位数字地址码+六位数字出生日期码+三位数字顺序码
 * @param idCard
 */
function maskIdCardNormal(idCard) {
  if (![15, 18].includes(idCard.length)) {
    throw new Error('身份证位数不正确');
  }
  return idCard.length === 15 ? idCard.replace(/(\w{1})\w{3}(\w{6})\w{4}(\w{1})/, '$1***$2****$3') : idCard.replace(/(\w{1})\w{3}(\w{8})\w{5}(\w{1})/, '$1***$2*****$3');
}

/**
 * 【浙里办脱敏规范】手机号脱敏：显示前三位、后四位，中间*号
 * @param cellphone
 */
function maskCellphone(cellphone) {
  return mask(cellphone, {
    before: 3,
    after: 4
  });
}

/**
 * 【浙里办脱敏规范】固定电话脱敏：显示区号和后面四位
 * @param fixedPhone
 */
function maskFixedPhone(fixedPhone) {
  const splited = fixedPhone.split('-');
  const masked = mask(splited[1], {
    after: 4
  });
  return [splited[0], masked].join('-');
}

/**
 * 【浙里办脱敏规范】邮箱脱敏
 * @ 前面字符显示三位，@ 后面完整显示
 * @ 前面字符少于三位则全部显示，@ 前面加***
 * @param email
 */
function maskEmail(email) {
  const splited = email.split('@');
  const masked = splited[0].length <= 3 ? '***' : mask(splited[0], {
    before: 3
  });
  return [masked, splited[1]].join('@');
}

/**
 * 【浙里办脱敏规范】银行卡号脱敏-银行卡号：显示前六位后四位，中间*号
 * @param bankCard
 */
function maskBankCard(bankCard) {
  return mask(bankCard, {
    before: 6,
    after: 4
  });
}